package cn.leonis.leetcode;

/**
 * 删除排序数组中的重复项
 */
public class RemoveDuplicatesSolution {

    public static void main(String[] args) {
        RemoveDuplicatesSolution solution = new RemoveDuplicatesSolution();
        int[] nums = new int[]{0, 1, 1, 2, 3, 3, 3, 4};
        int l = solution.removeDuplicates(nums);
        System.out.println("l = " + l);
        for (int i = 0; i < l; i++) {
            System.out.println(nums[i]);
        }
    }

    /**
     * 使用指针的方式进行寻找
     * @param nums
     * @return
     */
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0 || nums.length < 2) {
            return nums.length;
        }

        int index = 0;

        for (int i = 1; i < nums.length; i++) {
            if (nums[index] != nums[i]) {
                nums[++index] = nums[i];
            }
        }

        return ++index;
    }
}
